package scavenger.client;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Text;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import java.lang.*; 
import java.net.*;
import java.io.*;

/** The Action class is a simple example of a TCP/IP Socket Client.
 *
 */
public class Action {

	String PlayerID;
	String ActionID;
	float gpsX, gpsY;
	float accelX, accelY, accelZ;
	
    //String id;
    int value;

    Action(){

	try{

	    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
	    
	    System.out.print("actionId: ");
	    this.ActionID = in.readLine();
	    
	    System.out.print("actionVal: ");
	    Integer thisInt = new Integer(in.readLine());
	    this.value = thisInt.intValue();

	}
	catch(IOException ioe){}

    }
    
    Action(String pID, String aID, float X, float Y){
    	this.PlayerID = pID;
    	this.ActionID = aID;
    	this.gpsX = X;
    	this.gpsY = Y;
    }
    Action(String pID, String aID, float X, float Y, float aX, float aY, float aZ){
    	this.PlayerID = pID;
    	this.ActionID = aID;
    	this.gpsX = X;
    	this.gpsY = Y;
    	this.accelX = aX;
    	this.accelY = aY;
    	this.accelZ = aZ;
    }


    Action(String id, int value){
	this.ActionID = id;
	this.value = value;
    }


    Action(Element el){
	this.ActionID = getTextValue(el,"ActionID");
	this.value = getIntValue(el,"ActionValue"); 
    }


    private String getTextValue(Element ele, String tagName) {
	String textVal = null;
	NodeList nl = ele.getElementsByTagName(tagName);
	if(nl != null && nl.getLength() > 0) {
	    Element el = (Element)nl.item(0);
	    textVal = el.getFirstChild().getNodeValue();
	}
	
	return textVal;
    }
    
    /**
     * Calls getTextValue and returns a int value
     * @param ele
     * @param tagName
     * @return
     */
    private int getIntValue(Element ele, String tagName) {
	return Integer.parseInt(getTextValue(ele,tagName));
    }

    public String getId(){
	return(this.ActionID);
    }

    public int getValue(){
	return(this.value);
    }

    public String toString(){

	StringBuffer sb = new StringBuffer();
	sb.append(ActionID + " " + value);
	return sb.toString();
    }


    
    /**                                                                                                                   
     * Helper method which creates a XML element
     * @param b The book for which we need to create an xml representation
     * @return XML element snippet representing an action
     */
    public Element toXmlElement(Document dom){
	
	Element actionEle = dom.createElement("Action");
	
	//PlayerID
	Element elem = dom.createElement("PlayerID");
	elem.setAttribute("value", PlayerID);
	actionEle.appendChild(elem);
	
	//ActionID
	elem = dom.createElement("ActionID");
	elem.setAttribute("value", ActionID);
	actionEle.appendChild(elem);
	
	//GPS Location
	elem = dom.createElement("GPS");
	elem.setAttribute("Lat", String.valueOf(gpsX) );
	elem.setAttribute("Long", String.valueOf(gpsY) );
	actionEle.appendChild(elem);
	
	return actionEle;
	
	/*
	//create author element and author text node and attach it to bookElement
	Element idEle = dom.createElement("ActionID");
	Text idText = dom.createTextNode(getId());
	idEle.appendChild(idText);
	actionEle.appendChild(idEle);
	
	//create title element and title text node and attach it to bookElement
	Element valueEle = dom.createElement("ActionValue");
	Text valueText = dom.createTextNode( (new Integer(getValue())).toString());
	valueEle.appendChild(valueText);
	actionEle.appendChild(valueEle);
	*/
	

	
    }



    
}